package com.yoooya.base.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.cache.ehcache.EhCacheCacheManager;
import org.springframework.cache.ehcache.EhCacheManagerFactoryBean;
import org.springframework.context.EnvironmentAware;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.springframework.core.io.support.ResourcePatternResolver;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.listener.RedisMessageListenerContainer;
import redis.clients.jedis.JedisPoolConfig;

//@Configuration
//@EnableCaching
public class CacheConfig /*implements EnvironmentAware */{

	/*public static final String WX_USER_INFO_CACHE = "wxUserInfoCache";
	public static final String REGION_CACHE = "region";
	public static final String REGION_CACHE_LIST = "region_list";
	public static final String CONFIG_CACHE = "config";

	private Environment environment;

	@Autowired
	private ResourcePatternResolver resourcePatternResolver;

	@Override
	public void setEnvironment(Environment environment) {
		this.environment = environment;
	}

	*//*@Bean
	public EhCacheManagerFactoryBean cacheManagerFactory() {
		EhCacheManagerFactoryBean bean = new EhCacheManagerFactoryBean();
		bean.setConfigLocation(resourcePatternResolver.getResource("classpath:ehcache.xml"));
		return bean;
	}*//*

	*//*@Bean
	public CacheManager cacheManager() {
		EhCacheCacheManager bean = new EhCacheCacheManager();
		bean.setCacheManager(cacheManagerFactory().getObject());
		return bean;
	}*//*

	@Bean
	public JedisConnectionFactory redisConnectionFactory() {
		JedisConnectionFactory redisConnectionFactory = new JedisConnectionFactory();
		redisConnectionFactory.setHostName(environment.getRequiredProperty("cache.host.name"));
		redisConnectionFactory.setPort(environment.getProperty("cache.port", int.class, 6379));
		redisConnectionFactory.setPassword(environment.getProperty("cache.password"));
		JedisPoolConfig config = new JedisPoolConfig();
		config.setMaxWaitMillis((long) environment.getProperty("cache.max.wait.millis", int.class, 30000));
		config.setMaxTotal(environment.getProperty("cache.max.total", int.class, 500));
		config.setMaxIdle(environment.getProperty("cache.max.idle", int.class, 200));
		config.setMinIdle(environment.getProperty("cache.min.idle", int.class, 20));
		config.setTestOnBorrow(false);
		config.setTestOnReturn(false);
		config.setTestWhileIdle(true);
		redisConnectionFactory.setPoolConfig(config);
		return redisConnectionFactory;
	}

	@Bean
	public StringRedisTemplate stringRedisTemplate(RedisConnectionFactory rcf) {
		StringRedisTemplate redisTemplate = new StringRedisTemplate();
		redisTemplate.setConnectionFactory(rcf);
		redisTemplate.setEnableTransactionSupport(true);
		return redisTemplate;
	}

	@Bean
	public RedisTemplate redisTemplate(RedisConnectionFactory rcf) {
		RedisTemplate redisTemplate = new RedisTemplate();
		redisTemplate.setConnectionFactory(rcf);
		redisTemplate.setEnableTransactionSupport(true);
		return redisTemplate;
	}

	@Bean(destroyMethod = "destroy")
	public RedisMessageListenerContainer topicContainer() {
		RedisMessageListenerContainer container = new RedisMessageListenerContainer();
		container.setConnectionFactory(redisConnectionFactory());
		return container;
	}*/
}
